package com.platform.common.config;

import com.platform.common.menus.ResultCodeEnum;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * Swagger配置文件
 * @author rainly
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {

        //配置全局访问状态码
        List<ResponseMessage> responseMessageList = new ArrayList<>();
        responseMessageList.add(new ResponseMessageBuilder()
                .code(ResultCodeEnum.NOT_FOUND.getCode())
                .message(ResultCodeEnum.NOT_FOUND.getMessage())
                .build());
        responseMessageList.add(new ResponseMessageBuilder()
                .code(ResultCodeEnum.FORBIDDEN.getCode())
                .message(ResultCodeEnum.FORBIDDEN.getMessage())
                .build());
        responseMessageList.add(new ResponseMessageBuilder()
                .code(ResultCodeEnum.UNAUTHORIZED.getCode())
                .message(ResultCodeEnum.UNAUTHORIZED.getMessage())
                .build());
        responseMessageList.add(new ResponseMessageBuilder()
                .code(ResultCodeEnum.SUCCESS.getCode())
                .message(ResultCodeEnum.SUCCESS.getMessage())
                .build());
        responseMessageList.add(new ResponseMessageBuilder()
                .code(ResultCodeEnum.FAILURE.getCode())
                .message(ResultCodeEnum.FAILURE.getMessage())
                .build());


        /*
        responseMessageList.add(new ResponseMessageBuilder().code(HttpServletResponse.SC_NOT_FOUND).message("找不到资源").responseModel(new ModelRef("ApiError")).build());
        responseMessageList.add(new ResponseMessageBuilder().code(HttpServletResponse.SC_FORBIDDEN).message("禁止访问").responseModel(new ModelRef("ApiError")).build());
        responseMessageList.add(new ResponseMessageBuilder().code(HttpServletResponse.SC_UNAUTHORIZED).message("未经授权").responseModel(new ModelRef("ApiError")).build());
        responseMessageList.add(new ResponseMessageBuilder().code(HttpServletResponse.SC_OK).message("成功").responseModel(new ModelRef("ApiError")).build());
        */
        return new Docket(DocumentationType.SWAGGER_2)
                .globalResponseMessage(RequestMethod.DELETE,responseMessageList)
                .globalResponseMessage(RequestMethod.GET,responseMessageList)
                .globalResponseMessage(RequestMethod.PUT,responseMessageList)
                .globalResponseMessage(RequestMethod.POST,responseMessageList)
                .apiInfo(apiInfo())
                .select()
                //为扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.platform.system.controller"))
                //过滤功能 可以使用  any（），none（），regex（）或  ant（）
                .paths(PathSelectors.any())
                //还有OAuth安全的配置未展示
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //大标题
                .title("统一管理平台")
                // 版本号
                .version("1.0")
                //描述
                .description("统一管理平台的接口文档")
                .contact(new Contact("jqlin", "4bboy.top", "610232785@qq.com"))
                //.license("The Apache License, Version 2.0")
                //.licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
                .termsOfServiceUrl("http://blog.didispace.com/")
                .build();
    }
}
